/**
 * 初始化
 * @param self
 * @param tiyData
 */
export const init = (self, tiyData) => {
  tiyData.tPage.total = 1;
  tiyData.tPage.startIndex = 0;
  tiyData.tPage.stepNumber = 40;
  tiyData.tPage.currentPage = 1;
  tiyData.tParam.couponType = 0;
  tiyData.tParam.couponTitle = "";
  tiyData.tParam.couponId = "";
  tiyData.tParam.isGetAll = false;
  tiyData.tParam.userName = "";
};


/**
 * 查看优惠券详情
 * @param self
 * @param tiyData
 */
export const modify = (self,tiyData,id) => {
  self.$Spin.show();
  self.$apiCoupon.getCouponDetailById({id:id}).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
    tiyData.tStatus.modalModify.isShow = true;
    tiyData.tStatus.modalModify.isLoading = true;
    tiyData.tParam.couponDetail = response.result;
    //权限列表json
    let roles = self.$toJson(response.result.distributionRoles);
    tiyData.tParam.couponDetail.distributionRoles = roles;
    //构造权限tree
    tiyData.tData.roleTreeData.splice(0,tiyData.tData.roleTreeData.length);
    if(roles.length>0){
      tiyData.tData.roleList.forEach(function(item){
        let i = roles.filter(j=>j.id ==item.id);
        if(i!=null&&i!=''){
          tiyData.tData.roleTreeData.push({title:item.name,id:item.id,checked:true})
        }else{
          tiyData.tData.roleTreeData.push({title:item.name,id:item.id})
        }

      })
    }
      tiyData.tParam.couponDetail.couponCode = self.$toJson(response.result.couponCode);

    } else {
      self.$Message.error(response.errorMessage);
    }
  });
};


  /**
   * 查询优惠券列表
   * @param self
   * @param tiyData
   */
  export const webList = (self, tiyData) => {
    self.$Spin.show();
    self.$apiCoupon.getCouponDetailList({
      "couponType":tiyData.tParam.couponType,
      "pageNum":tiyData.tPage.currentPage,
      "pageSize":tiyData.tPage.stepNumber,
      "isGetAll" : tiyData.tParam.isGetAll,
      "userName":tiyData.tParam.userName,
      "couponTitle":tiyData.tParam.couponTitle,
    }).then(function (response){
      tiyData.tData.couponDetailList.splice(0, tiyData.tData.couponDetailList.length);
      self.$Spin.hide();
      if (response.success) {
        let couponPage = response.result;
        tiyData.tPage.total = couponPage.total;
        tiyData.tPage.stepNumber = couponPage.pageSize;
        couponPage.list.forEach(
          function (item) {
            tiyData.tData.couponDetailList.push(item);
          }
        );
      } else {
        self.$Message.error(response.errorMessage);
      }
    });
};

/**撤回优惠券*/
export const revokeCoupon = (self, tiyData) => {
  self.$Modal.confirm({
    title: "确认",
    content: "确认撤回此优惠券吗?",
    okText: "撤回",
    onOk: () => {
      self.$Spin.show();
      self.$apiCoupon.revokeCoupon({id: tiyData.tParam.couponDetailId}).then(response => {
        self.$Spin.hide();
        if(response.success) {
          self.$Message.success({duration: 3, content: "撤回成功!"});
          webList(self, tiyData);
        }else{
          self.$Message.error({duration: 3, content: response.errorMessage});
        }
      }).catch(() => {
        self.$Spin.hide();
        self.$Message.error({duration: 3, content: "撤回失败，请重试！"});
      });
    }
  });
}

/**优惠券详情*/
export const getCouponDetail = (self, tiyData) => {
    self.$Spin.show();
    self.$apiCoupon.getCouponDetail({id: tiyData.tParam.couponDetailId}).then(response => {
      self.$Spin.hide();
      if(response.success) {
        self.couponDetail = response.result;
      }else{
        self.$Message.error({duration: 3, content: response.errorMessage});
      }
    }).catch(() => {
      self.$Spin.hide();
      self.$Message.error({duration: 3, content: "查询失败，请重试！"});
    });
}
